﻿using System.Collections.Generic;
using System.Data;
using Ue.Infrastructure.DomainBase;
using Ue.Infrastructure.RepositoryFramework.Repository;
using Ue.Qcdz.Domain.feedbacks;

namespace Ue.Qcdz.Repositories.Feedbacks
{
    public class FeedbackRepository : SqlServerRepository<Feedback>, IFeedBackRepository
    {
        #region Overrides of SqlServerRepositoryBase<Feedback>

        protected override string GetDataTableName()
        {
            return "Comments";
        }
        protected override string GetKeyFieldName()
        {
            return base.GetKeyFieldName();
        }
        protected override string[] GetBaseInsertFields()
        {
            return new string[]
                       {
                           FeedbackFactory.FieldNames.EntityKey,
                           FeedbackFactory.FieldNames.Title,
                           FeedbackFactory.FieldNames.Comment,
                           FeedbackFactory.FieldNames.CurrentIp,
                           FeedbackFactory.FieldNames.Name,
                           FeedbackFactory.FieldNames.ShopId,
                           FeedbackFactory.FieldNames.Contact
        };
        }

        protected override string[] GetBaseUpdateFields()
        {
            return new string[]
                       {
                           FeedbackFactory.FieldNames.Title,
                           FeedbackFactory.FieldNames.Comment,
                           FeedbackFactory.FieldNames.CurrentIp,
                           FeedbackFactory.FieldNames.Name,
                           FeedbackFactory.FieldNames.ShopId,
                           FeedbackFactory.FieldNames.Contact
        };
        }

        protected override IDataParameter[] BuildParameters(Feedback entity)
        {
            return new IDataParameter[]
                       {
                           RepositoryDriver.BuildParameter(FeedbackFactory.FieldNames.EntityKey,entity.Key),
                           RepositoryDriver.BuildParameter(FeedbackFactory.FieldNames.Comment,entity.Comment),
                           RepositoryDriver.BuildParameter(FeedbackFactory.FieldNames.Contact,entity.Contact),
                           RepositoryDriver.BuildParameter(FeedbackFactory.FieldNames.CurrentIp,entity.CurrentIp),
                           RepositoryDriver.BuildParameter(FeedbackFactory.FieldNames.Name,entity.Name),
                           RepositoryDriver.BuildParameter(FeedbackFactory.FieldNames.ShopId,entity.ShopId),
                           RepositoryDriver.BuildParameter(FeedbackFactory.FieldNames.Title,entity.Title)
                       };
        }

        protected override IWhereClause BuildWhereClauseBy(IDataQuery dataQuery)
        {
            return null;
        }

        protected override IDictionary<string, ChildDataHandler> GetBuildEntityChildCallbacks()
        {
            return null;
        }

        #endregion

        #region Implementation of IFeedBackRepository

        public IList<Feedback> FindBy(string shopId)
        {
            var query = "SELECT * FROM Comments WHERE ShopId LIKE @ShopId";
            var parameter = new IDataParameter[]
                                {
                                    this.RepositoryDriver.BuildParameter(FeedbackFactory.FieldNames.ShopId,shopId)
                                };
            return this.BuildEntitiesFromSql(query, parameter);
        }

        #endregion
    }
}
